package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.common.util.StringUtil;
import org.footballmanager.pandafootball.system.domain.FootGp;
import org.footballmanager.pandafootball.system.domain.FootPerson;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.domain.FootUserPerson;
import org.footballmanager.pandafootball.system.form.impl.FootGpSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootGpMapper;
import org.footballmanager.pandafootball.system.service.FootGpService;
import org.footballmanager.pandafootball.system.service.FootPersonService;
import org.footballmanager.pandafootball.system.service.FootUserPersonService;
import org.footballmanager.pandafootball.system.service.FootZnxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

@Slf4j
@Service
public class FootGpServiceImpl extends ServiceImpl<FootGp> implements FootGpService {
    @Autowired
    private FootGpMapper footGpMapper;
    @Autowired
    private FootUserPersonService footUserPersonService;
    @Autowired
    private FootPersonService footPersonService;
    @Autowired
    private FootZnxService footZnxService;

    /**
     * 删除挂牌记录
     *
     * @param id      主键
     * @param request 请求
     * @return 是否成功
     */
    @Override
    @Transactional
    public int delete(Serializable id, HttpServletRequest request) {
        FootGp t = this.get(id);
        FootUserPerson fup = footUserPersonService.get(t.getFupId());
        if (null != fup) {
            fup.setGpzt(0);
            footUserPersonService.updateNotNull(fup, request);
        }
        log.info("删除球员挂牌:" + t.toString());
        return super.delete(id, request);
    }

    /**
     * 报分挂牌记录
     *
     * @param t       实体类
     * @param request 请求
     * @return 是否成功
     */
    @Override
    @Transactional
    public int save(FootGp t, HttpServletRequest request) {
        if (request != null) {
            FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
            FootPerson person = footPersonService.get(t.getQyId());
            String msg = user.getNike() + "已经挂牌球员" + StringUtil.getFullNameFromPerson(person) + ",价格为" + t.getYkj();
            footZnxService.sendZnx(user.getId(), null, msg, null, "/hy/footGp/search", request);
        }
        return super.save(t, request);
    }

    /**
     * 查找没成功挂牌的信息
     *
     * @return 没成功挂牌的记录
     */
    @Override
    public List<FootGp> findGpNotSuccess() {
        FootGpSearchForm footGpSearchForm = new FootGpSearchForm();
        footGpSearchForm.setCj(false);
        return this.findall(footGpSearchForm);
    }

    /**
     * 清理挂牌表里的数据
     *
     * @param request 请求
     */
    @Override
    public void clear(HttpServletRequest request) {
        List<FootGp> list = this.getall();
        for (FootGp gp : list) {
            FootUserPerson fup = footUserPersonService.get(gp.getFupId());
            if (null == fup || fup.getXycs() <= 0) {
                this.delete(gp.getId(), request);
            }
        }
    }

    /**
     * 根据球员主键和用户主键删除记录
     *
     * @param qyId    球员主键
     * @param userId  用户主键
     * @param request 请求
     */
    @Override
    public void deleteByQyidAndUserId(Long qyId, Long userId, HttpServletRequest request) {
        Example example = new Example(FootGp.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("qyId", qyId);
        criteria.andEqualTo("sellUserId", userId);
        criteria.andEqualTo("delFlag", 0);
        List<FootGp> list = this.findall(example);
        if (list != null && !list.isEmpty()) {
            for (FootGp gp : list) {
                this.delete(gp.getId(), request);
            }
        }
    }

    /**
     * 翻页方法
     *
     * @param footGpSearchForm 条件
     * @return 列表
     */
    @Override
    public List<FootGp> findall(FootGpSearchForm footGpSearchForm) {
        return footGpMapper.findall(footGpSearchForm);
    }

    /**
     * 根据主键获取记录
     *
     * @param id 主键
     * @return 记录
     */
    @Override
    public FootGp get(Serializable id) {
        return footGpMapper.findById(id);
    }

    /**
     * 查询
     *
     * @param entityname       实体类名
     * @param map              参数
     * @param footGpSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootGpSearchForm footGpSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footGpSearchForm);
        Page<FootGp> page = PageHelper.startPage(footGpSearchForm.getPageIndex(), footGpSearchForm.getPageSize());
        List<FootGp> list = this.findall(footGpSearchForm);
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footGpSearchForm 条件
     * @param request          请求
     */
    @Override
    public void deleteAll(FootGpSearchForm footGpSearchForm, HttpServletRequest request) {
        if (footGpSearchForm.getSelectIds() != null) {
            String[] result = footGpSearchForm.getSelectIds().split(Constants.COMMA);
            for (String id : result) {
                this.delete(Long.parseLong(id), request);
            }
        }
    }

    /**
     * 根据球员主键删除记录
     *
     * @param qyId 球员主键
     */
    @Override
    public void deletePersonByQyid(Long qyId) {
        Example example = new Example(FootGp.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("qyId", qyId);
        criteria.andEqualTo("delFlag", 0);
        List<FootGp> list = this.findall(example);
        if (list != null && !list.isEmpty()) {
            for (FootGp gp : list) {
                this.delete(gp.getId(), null);
            }
        }
    }
}
